package learningThread.automic;

import java.util.Random;

public class TestAutomic {



    public static void main(String[] args) {
        try {
            getRamdom();
            Thread[] threads = new Thread[10];
            User user = new User("liuwz",26);
            AutomicUtils.setInitailVal(user.getAge());
            for (int i = 0; i < threads.length; i++) {
                threads[i] = new Thread(new Runnable() {
                    @Override
                    public void run() {
                        boolean result = AutomicUtils.compareAndSet(26, 27);
                        System.out.println("ThreadA:"+Thread.currentThread().getName() + ""+result);
                    }
                }, "thread"+i);
            }
            for (int i = 0; i < threads.length; i++) {
                threads[i].start();
            }

            Thread.sleep(3000);
            System.out.println("最终得到的结果:"+AutomicUtils.get());
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public static int getRamdom(){
        Random random = new Random ();
        boolean[]  bool = new boolean[14];
        int randInt = 0;
        for(int j = 0; j < 10 ; j++) {
            /**得到10个不同的随机数*/
            do{
                randInt  = random.nextInt(10);
            }while(bool[randInt]);
            bool[randInt] = true;
            System.out.println(randInt);
        }
        return randInt;
    }
}
